<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%cart}}".
 *
 * @property integer $id
 * @property integer $store_id
 * @property integer $user_id
 * @property integer $goods_id
 * @property integer $num
 * @property integer $addtime
 * @property integer $is_delete
 * @property string $attr
 */
class LevelPrice extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%level_price}}';
    }

    /**
     * @inheritdoc
     */
    // public function rules()
    // {
    //     return [
    //         [['store_id', 'user_id', 'goods_id', 'attr'], 'required'],
    //         [['store_id', 'user_id', 'goods_id', 'num', 'addtime', 'is_delete'], 'integer'],
    //         [['attr'], 'string'],
    //     ];
    // }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'store_id' => 'Store ID',
            'name' => '等级名称',
        ];
    }

    /**
     * 商品等级价格表 一对一关系
     */
    public function getGoodsLevelPrice()
    {
        return $this->hasOne(GoodsLevelPrice::className(),['level_id'=>'id']);
    }

    public function GetLevelPrice($id, $store_id)
    {
        $level_price = [];
        if(in_array($store_id, \Yii::$app->params['shouQuan']))
        {
            // $LevelPrice = new LevelPrice();
            $level_price = LevelPrice::find()->where(['store_id' => $store_id])
            ->with([
                'goodsLevelPrice' => function($query) use ($id, $store_id)
                {
                    if($id != "" && $store_id != "")
                    {
                        $query->where(['goods_id' => $id, 'store_id' => $store_id]);
                    }
                }
            ])
            ->asArray()
            ->all();
        }
        return $level_price;
    }
}
